import { getArticleDetail, addFav, removeFav, like, unlike, commentLike, commentUnlike } from "@/api/pages";
import { formatDate } from "@/common/utils";
export default {
  //获取文章详情
  getArticleDetail() {
    let { article_id, uid: user_id } = this;
    getArticleDetail({
      article_id,
      user_id,
    }).then((res) => {
      console.log(res);
      if (res.code == 0) {
        res.data.time = formatDate(res.data.time);
        this.article = res.data;
      } else {
        // this.$toast(res.msg);
      }
    });
  },
  //切换文章收藏
  toggleFav() {
    let {
      uid,
      article: { is_fav },
      article_id,
    } = this;
    if (!this.checkLogin()) return;
    if (is_fav) {
      // console.log(is_fav);

      //如果原来是收藏的  接口就调 取消点
      removeFav({
        article_id,
        user_id: uid,
      }).then((res) => {
        // console.log(res);
        this.$toast(res.msg);
        if (res.code == 0) {
          this.article.is_fav = false;
        }
      });
    } else {
      //没收藏 收藏
      addFav({
        article_id,
        user_id: uid,
      }).then((res) => {
        // console.log(res);
        this.$toast(res.msg);
        if (res.code == 0) {
          this.article.is_fav = true;
          // this.getCommentList();
        }
      });
    }
  },

  //切换文章点赞
  toggleLike() {
    let {
      uid,
      article: { is_like },
      article_id,
    } = this;
    if (!this.checkLogin()) return;

    // console.log(is_fav);

    if (is_like) {
      //如果原来是收藏的  接口就调 取消点
      unlike({
        article_id,
        user_id: uid,
      }).then((res) => {
        // console.log(res);
        this.$toast(res.msg);
        if (res.code == 0) {
          this.article.is_like = false;
        }
      });
    } else {
      //没收藏 收藏
      like({
        article_id,
        user_id: uid,
      }).then((res) => {
        // console.log(res);
        this.$toast(res.msg);
        if (res.code == 0) {
          this.article.is_like = true;
        }
      });
    }
  },

  //评论点赞
  toggleCommentLike(comment_id, is_like, index) {
    if (!this.checkLogin()) return;
    let { uid } = this;

    if (is_like) {
      //取消
      commentUnlike({
        comment_id,
        user_id: uid,
      }).then((res) => {
        console.log(res);
        this.$toast(res.msg);
        if (res.code == 0) {
          this.commentList[index].like_count = res.count;
          this.commentList[index].is_like = false;
        }
      });
    } else {
      commentLike({
        comment_id,
        user_id: uid,
      }).then((res) => {
        // console.log(res);
        this.$toast(res.msg);
        if (res.code == 0) {
          this.commentList[index].like_count = res.count;
          this.commentList[index].is_like = true;
        }
      });
    }
  },
};
